package org.bing.flex.error {
    import mx.collections.ArrayCollection;
    import mx.rpc.Fault;
    import mx.rpc.events.FaultEvent;

    import org.bing.engine.helper.SystemManager;

    public class ErrorManager {
        public static var MAX_ERROR_NUM:int=50;
        [Bindable]
        public static var errors:ArrayCollection=new ArrayCollection();

        public static function error(error:Error):void {
            var item:ErrorItem=new ErrorItem();
            item.date=new Date();
            item.subject=error.message;
            item.target=error;
            info(item);
        }

        public static function fault(fault:FaultEvent):void {
            var ft:Fault=fault.fault;
            var item:ErrorItem=new ErrorItem();
            item.date=new Date();
            item.errorId=ft.errorID;
            item.subject=ft.faultString
            item.target=ft;
            info(item);
        }

        private static function info(item:ErrorItem):void {
            var dialog:ErrorDialog=SystemManager.getErrorDialog();
            if (errors == null) {
                errors=new ArrayCollection();
            }
            errors.addItemAt(item, 0);
            if (errors.length > MAX_ERROR_NUM) {
                errors.removeItemAt(errors.length - 1);
            }
        }
    }
}